<?php
/**
 * Class that operate on table 'megaphone'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2015-05-26 14:36
 */
class MegaphoneMySqlDAO implements MegaphoneDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return MegaphoneMySql 
	 */
	public function load($megaphone){
		$sql = 'SELECT * FROM megaphone WHERE megaphone_id = "'.$megaphone->megaphone_id.'"';
		$sqlQuery = new SqlQuery($sql);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll($megaphone){
		$sql = 'SELECT * FROM megaphone order by '.$megaphone->order_by.' '.$megaphone->sort_order.' limit '.$megaphone->results_per_page.' offset '.$megaphone->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	public function all_megaphone($megaphone){
		$sql = 'SELECT * FROM megaphone where '.$megaphone->search_by.' like "%'.$megaphone->search_by_value.'%" order by '.$megaphone->order_by.' '.$megaphone->sort_order.' limit '.$megaphone->results_per_page.' offset '.$megaphone->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	public function megaphone_count($megaphone){
		$sql = 'SELECT count(*) FROM megaphone order by '.$megaphone->order_by.' '.$megaphone->sort_order.' limit '.$megaphone->results_per_page.' offset '.$megaphone->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	
	public function searchby_count($megaphone){
		$sql = 'SELECT count(*) FROM megaphone where '.$megaphone->search_by.' like "%'.$megaphone->search_by_value.'%" order by '.$megaphone->order_by.' '.$megaphone->sort_order.' limit '.$megaphone->results_per_page.' offset '.$megaphone->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM megaphone ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param megaphone primary key
 	 */
	public function delete($megaphone_id){
		$sql = 'DELETE FROM megaphone WHERE megaphone_id = "'.$megaphone->megaphone_id.'"';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function delete_megaphone($megaphone){
		$sql = 'UPDATE megaphone set status="'.$megaphone->status.'" WHERE megaphone_id = "'.$megaphone->megaphone_id.'"';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param MegaphoneMySql megaphone
 	 */
	public function insert($megaphone){
		$sql = 'INSERT INTO megaphone (megaphone_displayorder, megaphone_title, megaphone_starton, megaphone_expireon, megaphone_content, megaphone_image, created_date, updated_date) VALUES ("'.$megaphone->megaphone_displayorder.'", "'.$megaphone->megaphone_title.'", "'.$megaphone->megaphone_starton.'", "'.$megaphone->megaphone_expireon.'", "'.$megaphone->megaphone_content.'", "'.$megaphone->megaphone_image.'", "'.$megaphone->created_date.'", "'.$megaphone->updated_date.'")';
		$sqlQuery = new SqlQuery($sql);
		
		$id = $this->executeInsert($sqlQuery);	
		$megaphone->megaphone_id = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param MegaphoneMySql megaphone
 	 */
	public function update($megaphone){
		$sql = 'UPDATE megaphone SET megaphone_displayorder = ?, megaphone_title = ?, megaphone_starton = ?, megaphone_expireon = ?, megaphone_content = ?, megaphone_image = ?, updated_date = ?, status = ? WHERE megaphone_id = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($megaphone->megaphone_displayorder);
		$sqlQuery->set($megaphone->megaphone_title);
		$sqlQuery->set($megaphone->megaphone_starton);
		$sqlQuery->set($megaphone->megaphone_expireon);
		$sqlQuery->set($megaphone->megaphone_content);
		$sqlQuery->set($megaphone->megaphone_image);
		$sqlQuery->set($megaphone->updated_date);
		$sqlQuery->setNumber($megaphone->status);

		$sqlQuery->setNumber($megaphone->megaphone_id);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM megaphone';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByMegaphoneDisplayorder($value){
		$sql = 'SELECT * FROM megaphone WHERE megaphone_displayorder = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByMegaphoneTitle($value){
		$sql = 'SELECT * FROM megaphone WHERE megaphone_title = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByMegaphoneStarton($value){
		$sql = 'SELECT * FROM megaphone WHERE megaphone_starton = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByMegaphoneExpireon($value){
		$sql = 'SELECT * FROM megaphone WHERE megaphone_expireon = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByMegaphoneContent($value){
		$sql = 'SELECT * FROM megaphone WHERE megaphone_content = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByMegaphoneImage($value){
		$sql = 'SELECT * FROM megaphone WHERE megaphone_image = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedDate($value){
		$sql = 'SELECT * FROM megaphone WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedDate($value){
		$sql = 'SELECT * FROM megaphone WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatus($value){
		$sql = 'SELECT * FROM megaphone WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByMegaphoneDisplayorder($value){
		$sql = 'DELETE FROM megaphone WHERE megaphone_displayorder = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByMegaphoneTitle($value){
		$sql = 'DELETE FROM megaphone WHERE megaphone_title = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByMegaphoneStarton($value){
		$sql = 'DELETE FROM megaphone WHERE megaphone_starton = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByMegaphoneExpireon($value){
		$sql = 'DELETE FROM megaphone WHERE megaphone_expireon = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByMegaphoneContent($value){
		$sql = 'DELETE FROM megaphone WHERE megaphone_content = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByMegaphoneImage($value){
		$sql = 'DELETE FROM megaphone WHERE megaphone_image = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedDate($value){
		$sql = 'DELETE FROM megaphone WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedDate($value){
		$sql = 'DELETE FROM megaphone WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatus($value){
		$sql = 'DELETE FROM megaphone WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return MegaphoneMySql 
	 */
	protected function readRow($row){
		$megaphone = new Megaphone();
		
		$megaphone->megaphone_id = $row['megaphone_id'];
		$megaphone->megaphone_displayorder = $row['megaphone_displayorder'];
		$megaphone->megaphone_title = $row['megaphone_title'];
		$megaphone->megaphone_starton = $row['megaphone_starton'];
		$megaphone->megaphone_expireon = $row['megaphone_expireon'];
		$megaphone->megaphone_content = $row['megaphone_content'];
		$megaphone->megaphone_image = $row['megaphone_image'];
		$megaphone->created_date = $row['created_date'];
		$megaphone->updated_date = $row['updated_date'];
		$megaphone->status = $row['status'];

		return $megaphone;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return MegaphoneMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>